package com.yesep.learn.jwt.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;

import com.yesep.learn.jwt.common.R;
import com.yesep.learn.jwt.config.JWTTokenConfig;
import com.yesep.learn.jwt.util.ServletUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @Description:用户退出系统成功后 需要做的业务操作 当用户退出系统成功后则会进入到此类并执行相关业务
 */
@Slf4j
public class LogoutSuccessHandlerImpl extends SimpleUrlLogoutSuccessHandler implements LogoutSuccessHandler {

	private JWTTokenConfig jwtTokenConfig;

	public LogoutSuccessHandlerImpl(JWTTokenConfig jwtTokenConfig) {
		super();
		this.jwtTokenConfig = jwtTokenConfig;
	}

	@Override
	public void onLogoutSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException, ServletException {
		super.onLogoutSuccess(request, response, authentication);
		final String refererUrl = request.getHeader("Referer");
		log.debug(refererUrl);
		String token = jwtTokenConfig.parseToken(request);
		jwtTokenConfig.disabledToken(token);
		ServletUtils.writeValueAsString(R.me(HttpServletResponse.SC_OK, "退出成功"), response);
	}

}